<?php
function GetTrackInfo($sc, $trackid)
{
    $db = new Database();
    $db->Connect();

    $track = $db->SelectColumns($sc . '_tracks', 'Name, Length, Laps, Location, Last_Winner, Hotlap, Hotlap_Owner', 'ID_Track = ' . $trackid);

    $db->Disconnect();

    if($track)
    {
        return '<b>' . $track[0]->Name . ' (' . $track[0]->Location . ')</b><br />Länge: ' . $track[0]->Length . '  Runden: ' . $track[0]->Laps
                . '<br />Letzter Sieger: ' . $track[0]->Last_Winner . ' Hotlap: ' . $track[0]->Hotlap_Owner . ' (' . $track[0]->Hotlap . ')';
    }
    else
    {
       return 'Could not load track data.';
    }   
}

function GetPicture($sc, $picid)
{
    $db = new Database();
    $db->Connect();

    $picture = $db->SelectColumns($sc . '_pictures', 'URL', 'ID_Picture = ' . $picid);

    $db->Disconnect();

    if($picture)
    {
        return '<img src="' . $picture[0]->URL . '" />';
    }
}

function ShowResults()
{
    
}

function ListRaces($sc)
{
    $db = new Database();
    $db->Connect();

    $races = $db->SelectColumns($sc . '_calendar', 'Track_ID, Start_Date, Finished, Picture_ID', 'ID_Series = ' . $id, 'Start_Date');

    if($races)
    {
        echo '<table>';
        $i = 1;
        foreach($races as $race)
        {
            echo '<tr><td><b>Rennen ' . $i . ' (' . $race->Start_Date . ')</b></td>';   
            echo '<td>' . GetPicture($sc, $race->Picture_ID) . '</td>';
            echo '<td>' . GetTrackInfo($sc, $race->Track_ID) . '</td>';
            echo '<td>';
            if($race->Finished == 1)
            {
                ShowResults();
            }
            else
            {
                echo 'Keine Ergebnisse';
            }
            echo '</td></tr>';
            $i++;
        }
        echo '</table>';
    }
    else
    {
        echo 'Der Rennkalender ist leer.';
    }
    $db->Disconnect();
}

if(isset($_GET['id']))
    {
        $id = $_GET['id'];
        require_once('../Database/Database.php');
        $db = new Database();
        $db->Connect();
        $shortcut = $db->SelectColumns('Series', 'Shortcut', 'ID_Series = ' . $id);
        if($shortcut)
        {
            $shortcut = $shortcut[0]->Shortcut;
?>
<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="css/default.css" />
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Rennkalender | Ranking - ALPHA</title>
    </head>
    <body>
        <div id="header">Rennkalender</div>
        <div id="main">
            <?php ListRaces($shortcut) ?>
        </div>
        <div id="footer"></div>

    </body>
</html>
<?php
        }
        else
        {
            echo 'Ung&uuml;ltige Serien ID.';
        }
    }
    else
    {
        echo 'ERROR';
    }
?>